package com.guodu.service.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.guodu.dao.PurchasingInformationThreeDao;
import com.guodu.dao.UnitsDao;
import com.guodu.dao.UserInfoDao;
import com.guodu.entity.PurchasingInformationThree;
import com.guodu.entity.PurchasinginforthreeStatus;
import com.guodu.entity.Units;
import com.guodu.entity.UserInfo;
import com.guodu.service.PurchasingInformationThreeService;
import com.guodu.vo.PurchasingInformationThreeQueryVo;
import com.guodu.vo.PurchasingInformationThreeVo;

@Service("purchasingInformationThreeService")
public class PurchasingInformationThreeServiceImpl implements PurchasingInformationThreeService {

	
	@Resource
	private PurchasingInformationThreeDao purchasingInformationThreeDao;
	
	@Resource
	private UnitsDao unitsDao;
	
	@Resource
	private UserInfoDao userInfoDao;
	
	
	@Override
	public long findSizeByVo(PurchasingInformationThreeQueryVo pitq, Units units) {
		// TODO Auto-generated method stub
		return purchasingInformationThreeDao.findSizeByVo(pitq,units);
	}

	@Override
	public List<PurchasingInformationThreeVo> findByVo(PurchasingInformationThreeQueryVo pitq, Units units, int curPage,
			int pageSize) {
		// TODO Auto-generated method stub
		List<PurchasingInformationThree> list = purchasingInformationThreeDao.findByVo(pitq,units, curPage, pageSize);
		List<PurchasingInformationThreeVo> purchasingInformationThreeVos = new ArrayList<PurchasingInformationThreeVo>();
		for(PurchasingInformationThree purchasingInformationThree : list ){
			PurchasingInformationThreeVo purchasingInformationThreeVo = new PurchasingInformationThreeVo(purchasingInformationThree);
			UserInfo userInfo = userInfoDao.findById(purchasingInformationThree.getCreatePeople());
			Units unitsInfo = unitsDao.findById(userInfo.getUnitsId());
			purchasingInformationThreeVo.setUnitsName(unitsInfo.getUnitsName());
			purchasingInformationThreeVo.setIndustry(unitsInfo.getIndustry());
			if(unitsInfo.getFunitsId()!=null){
				Units funitsInfo = unitsDao.findById(unitsInfo.getFunitsId());
				purchasingInformationThreeVo.setFunitsName(funitsInfo.getUnitsName());
			}
			purchasingInformationThreeVo.setUserName(userInfo.getUserName());
			purchasingInformationThreeVos.add(purchasingInformationThreeVo);
		}
		return purchasingInformationThreeVos;
	}

	@Override
	public void save(PurchasingInformationThree purchasingInformationThree) {
		// TODO Auto-generated method stub
		purchasingInformationThreeDao.save(purchasingInformationThree);
	}

	@Override
	public PurchasingInformationThree findById(String purchaseId) {
		// TODO Auto-generated method stub
		return purchasingInformationThreeDao.findById(purchaseId);
	}

	@Override
	public void update(PurchasingInformationThree purchasingInformationThree,Long status) {
		// TODO Auto-generated method stub
		purchasingInformationThreeDao.update(purchasingInformationThree);
		List<PurchasinginforthreeStatus> entities = new ArrayList<PurchasinginforthreeStatus>();
		PurchasinginforthreeStatus entity = new PurchasinginforthreeStatus();
		entity.setAfterStatus(Long.valueOf(0));
		entity.setBeforeStatus(status);
		entity.setCreatePeople(purchasingInformationThree.getCreatePeople());
		entity.setPurchaseId(purchasingInformationThree.getPurchaseId());
		entity.setStatusId("1");
		entity.setCreateTime(Calendar.getInstance().getTime());
		entities.add(entity);
		purchasingInformationThreeDao.changeStatus(entities);
	}

	@Override
	public List<PurchasinginforthreeStatus> findStatus(String purchaseId) {
		// TODO Auto-generated method stub
		return purchasingInformationThreeDao.findStatus(purchaseId);
	}

	@Override
	public void examine(String ids, String rs, String beforeStatus,
			Long status, UserInfo user) {
		// TODO Auto-generated method stub
		String[] idses = ids.split(",");
		String[] beforeStatuses = beforeStatus.split(",");
		List<PurchasinginforthreeStatus> entities = new ArrayList<PurchasinginforthreeStatus>();
		for(int i=0;i<idses.length;i++){
			PurchasinginforthreeStatus entity = new PurchasinginforthreeStatus();
			entity.setAfterStatus(status);
			entity.setBeforeStatus(Long.valueOf(beforeStatuses[i]));
			entity.setCreatePeople(user.getUserId());
			entity.setPurchaseId(idses[i]);
			entity.setReason(rs);
			entity.setStatusId(i+"");
			entity.setCreateTime(Calendar.getInstance().getTime());
			entities.add(entity);
		}
		purchasingInformationThreeDao.changeStatus(entities);
		purchasingInformationThreeDao.examine(ids,status);
	}

	@Override
	public void delByIds(String[] delIds) {
		// TODO Auto-generated method stub
		purchasingInformationThreeDao.delByIds(delIds);
	}

	@Override
	public List<PurchasingInformationThree> findAllByVo(
			PurchasingInformationThreeQueryVo pitq, Units attribute) {
		// TODO Auto-generated method stub
		return purchasingInformationThreeDao.findAllByVo(pitq,attribute);
	}

	@Override
	public void examine(PurchasingInformationThreeQueryVo pitq, Units units) {
		// TODO Auto-generated method stub
		purchasingInformationThreeDao.examin(pitq, units);
	}

	@Override
	public PurchasingInformationThreeVo findAllMoney(
			PurchasingInformationThreeQueryVo pitq, Units units) {
		// TODO Auto-generated method stub
		return purchasingInformationThreeDao.findAllMoney(pitq,units);
	}

}
